﻿# On actions for child birth and pregnancy notification events, triggered automatically by the game

# called for the mother
# scope:child is the new born character
# scope:mother is the pregnant mother
# scope:real_father is the biological father
# scope:father is the family father (not the biological)
on_birth_mother = {
	events = {
		shogunate_birth.0001 # Name a child using the letter unique to each clan (Tori-ji system)
		birth.0001 #No one dies - hidden event that selects visible birth event based on bastard status and complication rolls
		birth.3011 #Mother dies
	}
	effect = {

		scope:mother = {
			if = {
				limit = {
					is_playable_character = yes
					exists = scope:mother.location
					exists = scope:child.location
					scope:mother.location = scope:child.location
				}
				trigger_event = {
					id = bp2_yearly.4000
 	  				days = 3
				}
				# If you have a legend maybe we change it?
				if = {
					limit = {
						has_legend_chapter_trigger = { CHAPTER = motivation }
						NOT = {
							promoted_legend ?= {
								has_legend_chapter = {
									name = motivation
									localization_key = legend_chapter_motivation_child_mother
								}
							}
						}
					}
					random = {
						chance = 10
						send_interface_toast = {
							title = legend_chapter_change_toast
							left_icon = scope:mother
							right_icon = scope:child
							promoted_legend = {
								set_legend_chapter = {
									name = motivation
									localization_key = legend_chapter_motivation_child_mother
								}
							}
						}
					}
				}
			}
		}

		random = { # legend_spread_events.8170 follow-up
			chance = 10
			scope:mother = {
				trigger_event = {
					id = legend_spread_events.8171
					months = 3
				}
			}
			scope:father ?= {
				trigger_event = {
					id = legend_spread_events.8171
					months = 3
				}
			}
		}

		scope:mother.current_travel_plan ?= { 
			if = {
				limit = {
					travel_plan_owner = { is_ruler = yes }
				}
				add_companion = scope:child
			}
		}

		# Gain piety from Sacred Childbirth doctrine
		if = {
			limit = {
				faith = { has_doctrine_parameter = piety_from_childbirth_active }
			}
			send_interface_toast = {
				type = event_generic_neutral
				title = piety_from_childbirth_toast
				left_icon = scope:child
				right_icon = scope:mother
				if = {
					limit = {
						scope:child = { has_any_negative_bastard_trait_trigger = no }
					}
					add_piety = {
						value = major_piety_gain
						if = {
							limit = {
								any_child = { is_twin_of = scope:child }
							}
							add = major_piety_gain
						}
						if = {
							limit = {
								any_child = {
									count >= 2
									is_twin_of = scope:child
								}
							}
							add = major_piety_gain
						}
					}
				}
				else = {
					add_piety = {
						value = minor_piety_gain
						if = {
							limit = {
								any_child = { is_twin_of = scope:child }
							}
							add = minor_piety_gain
						}
						if = {
							limit = {
								any_child = {
									count >= 2
									is_twin_of = scope:child
								}
							}
							add = minor_piety_gain
						}
					}
				}
			}
		}
		scope:child = {
			if = {
				limit = { has_trait = twin }
				random_sibling = {
					limit = { is_twin_of = scope:child }
					save_scope_as = child_2
				}
			}
		}

		if = {
			limit = {
				NOR = {
					any_child = {
						even_if_dead = yes
						NOT = {
							this = scope:child
						}
					}
					exists = scope:child_2
				}
			}
			create_character_memory = {
				type = first_born
				participants = {
					child = scope:child
				}
			}
		}
		else = {
			if = {
				limit = { exists = scope:child_2 }
				create_character_memory = {
					type = twins_born
					participants = {
						child = scope:child
						child_2 = scope:child_2
					}
				}
			}
			else = {
				create_character_memory = {
					type = child_born
					participants = {
						child = scope:child
					}
				}
			}
		}

		scope:mother = {
			if = {
				limit = {
					has_character_modifier = oath_of_children
				}
				hidden_effect = {
					remove_character_modifier = oath_of_children
					add_character_modifier = oath_of_children
				}
			}
		}
	}
}

# called for the father
# scope:child is the new born character
# scope:mother is the pregnant mother
# scope:real_father is the biological father
# scope:father is the family father (not the biological)
on_birth_father = {
	events = {
	}
	effect = {
		scope:father = {
			if = {
				limit = {
					is_playable_character = yes
					exists = scope:father.location
					exists = scope:child.location
					scope:father.location = scope:child.location
				}
				trigger_event = {
					id = bp2_yearly.4000
 	  				days = 3
				}
				# If you have a legend maybe we change it?
				if = {
					limit = {
						has_legend_chapter_trigger = { CHAPTER = motivation }
						NOT = {
							promoted_legend ?= {
								has_legend_chapter = {
									name = motivation
									localization_key = legend_chapter_motivation_child_father
								}
							}
						}
					}
					random = {
						chance = 10
						send_interface_toast = {
							title = legend_chapter_change_toast
							left_icon = scope:father
							right_icon = scope:child
							promoted_legend = {
								set_legend_chapter = {
									name = motivation
									localization_key = legend_chapter_motivation_child_father
								}
							}
						}
					}
				}
			}
		}

		# Gain piety from Sacred Childbirth doctrine
		if = {
			limit = {
				faith = { has_doctrine_parameter = piety_from_childbirth_active }
			}
			send_interface_toast = {
				type = event_generic_neutral
				title = piety_from_childbirth_toast
				left_icon = scope:child
				right_icon = scope:mother
				if = {
					limit = {
						scope:child = { has_any_negative_bastard_trait_trigger = no }
					}
					add_piety = {
						value = major_piety_gain
						if = {
							limit = {
								any_child = { is_twin_of = scope:child }
							}
							add = major_piety_gain
						}
						if = {
							limit = {
								any_child = {
									count >= 2
									is_twin_of = scope:child
								}
							}
							add = major_piety_gain
						}
					}
				}
				else = {
					add_piety = {
						value = minor_piety_gain
						if = {
							limit = {
								any_child = { is_twin_of = scope:child }
							}
							add = minor_piety_gain
						}
						if = {
							limit = {
								any_child = {
									count >= 2
									is_twin_of = scope:child
								}
							}
							add = minor_piety_gain
						}
					}
				}
			}
		}
		scope:child = {
			if = {
				limit = { has_trait = twin }
				random_sibling = {
					limit = { is_twin_of = scope:child }
					save_scope_as = child_2
				}
			}
		}
		
		if = {
			limit = {
				NOR = {
					any_child = {
						even_if_dead = yes
						NOT = {
							this = scope:child
						}
					}
					exists = scope:child_2
				}
			}
			create_character_memory = {
				type = first_born
				participants = {
					child = scope:child
				}
			}
		}
		else = {
			if = {
				limit = { exists = scope:child_2 }
				create_character_memory = {
					type = twins_born
					participants = {
						child = scope:child
						child_2 = scope:child_2
					}
				}
			}
			else = {
				create_character_memory = {
					type = child_born
					participants = {
						child = scope:child
					}
				}
			}
		}

		scope:father = {
			if = {
				limit = {
					has_character_modifier = oath_of_children
				}
				hidden_effect = {
					remove_character_modifier = oath_of_children
					add_character_modifier = oath_of_children
				}
			}
		}
	}
}

# called for the biological father
# only called if father and real father are different
# scope:child is the new born character
# scope:mother is the pregnant mother
# scope:real_father is the biological father
# scope:father is the family father (not the biological)
on_birth_real_father = {
	events = {

	}
}

# called for the newborn child
# scope:child is the new born character
# scope:mother is the pregnant mother
# scope:father is the family father (not the biological)
# scope:real_father is the biological father
# scope:is_bastard, true if a known bastard
on_birth_child = {
	events = {
		middle_east_decisions.0012 #Saoshyant Descendants.
		major_decisions.0101 #Divine/Consecrated Blood Descendants.
		birth.8001 #Reincarnation
		birth.8011 #Born in the Purple
		trait_specific.0500 #To handle genetic traits not expressed until later in life
		debug.0010 # Disease data tracking
	}
	effect = {
		# MPO: Nomadic Philosophy Inheritance
		if = {
			limit = {
				NOT = { has_trait = nomadic_philosophy }
				OR = {
					scope:mother = {
						has_trait = nomadic_philosophy
						NOT = { has_character_flag = nomphil_exodus_first_gen }
					}
					scope:father ?= {
						has_trait = nomadic_philosophy
						NOT = { has_character_flag = nomphil_exodus_first_gen }
					}
				}
			}
			add_trait = nomadic_philosophy
			if = {
				limit = {
					NOR = {
						location.county ?= {
							any_county_situation = { this = situation:the_great_steppe }
						}
						top_liege ?= {
							government_has_flag = government_is_nomadic
						}
					}
				}
				add_character_flag = nomphil_exodus_first_gen
			}
		}
		if = {
			limit = {
				scope:mother = {
					has_character_flag = blessed_baby
				}
			}
			random_list = {
				23 = {
					add_trait = intellect_good_2
				}
				10 = {
					add_trait = intellect_good_3
				}
				23 = {
					add_trait = physique_good_2
				}
				10 = {
					add_trait = physique_good_3
				}
				23 = {
					add_trait = beauty_good_2
				}			
				10 = {
					add_trait = beauty_good_3
				}
			}
		}
		# Make sure that children aren't Lowborn if one of their parents are, in fact, of noble stock
		if = {
			limit = {
				OR = {
					scope:is_bastard = no
					faith = { has_doctrine_parameter = bastards_none }
				}
				scope:child = {
					is_lowborn = yes
				}
				OR = {
					AND = {
						exists = scope:father
						scope:father = {
							is_lowborn = no
						}
					}
					scope:mother = {
						is_lowborn = no
					}
				}
			}
			if = {
				limit = {
					NOT = {
						exists = scope:father
					}
					exists = mother.house
				}
				set_house = mother.house
			}
			else_if = {
				limit = {
					exists = scope:father
					exists = mother.house
					scope:father = {
						OR = {
							AND = {
								is_spouse_of = scope:mother
								matrilinear_marriage = yes
							}
							is_concubine_of = scope:mother
						}
					}
				}
				set_house = mother.house
			}
			else_if = {
				limit = {
					exists = scope:father
					exists = father.house
				}
				set_house = father.house
			}
			else = {
				set_house = mother.house
			}
		}
	
		#Add bastardy traits and set correct House
		if = {
			limit = {
				scope:is_bastard = yes
			}
			add_bastard_trait_based_on_faith_effect = yes

			if = {
				limit = {
					scope:mother = {
						has_character_flag = pregnancy_real_father_of_bastard_is_known_flag
					}
				}
				#Set father and house
				set_father = scope:real_father
			}
			set_parent_house_effect = yes

			#Set conditions for any twins
			scope:mother = {
				every_child = {
					limit = {
						is_twin_of = scope:child
					}
					add_bastard_trait_based_on_faith_effect = yes
					if = {
						limit = {
							scope:mother = {
								has_character_flag = pregnancy_real_father_of_bastard_is_known_flag
							}
						}
						#Set father and house
						set_father = scope:real_father
					}
					set_parent_house_effect = yes
				}
			}
		}

		#Add house hook
		if = {
			limit = {
				is_alive = yes
				exists = scope:child.house
				scope:child.house.house_head = { is_alive =yes }
				NOT = {
					scope:child = scope:child.house.house_head # If the house dies out, a posthumous child can revive it
				}
				#No need to check if the hook can be added, the kid was just born
			}
			scope:child.house.house_head = {
				add_hook_no_toast = {
					type = house_head_hook
					target = scope:child
				}
			}
		}

		if = {
			limit = {
				exists = scope:father
				exists = scope:mother
				scope:mother = { is_concubine_of = scope:father }
			}
			add_trait = child_of_concubine_female
		}
		if = {
			limit = {
				exists = scope:father
				exists = scope:mother
				scope:father = { is_concubine_of = scope:mother }
			}
			add_trait = child_of_concubine_male
		}
		#create wet_nurse and nursed_child relation between the newborn and wet nurse court position character
		if = {
			limit = {
				scope:child.liege ?= {
					any_courtier = {
						has_court_position = wet_nurse_court_position
						save_temporary_scope_as = wet_nurse
					}
				}
				NOT = { any_relation = { type = wet_nurse } }
			}
			set_relation_wet_nurse = scope:wet_nurse
		}
		# Born hostages should be released if their dynasty is the same as their warden
		if = {
			limit = {
				is_hostage = yes
				dynasty = warden.dynasty
			}
			save_scope_as = unhostaged_child
			warden = { save_scope_as = warden }
			return_hostage = yes
			scope:warden = { add_courtier = root }
		}
		hidden_effect = {
			every_parent = {
				limit = { has_perk = sound_foundations_perk	}
				sound_foundations_skill_gain_effect = yes
			}
		}

		# Blood legacy 4 extra genetic trait chance effect
		hidden_effect = {
			if = {
				limit = {
					exists = scope:child.dynasty
					scope:child.dynasty = { has_dynasty_perk = blood_legacy_4 }
					OR = { # The child must have a real parent of the Dynasty...
						scope:mother.dynasty = scope:child.dynasty
						scope:real_father.dynasty = scope:child.dynasty
					}
				}
				if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_beauty_good_1
						}
						NOR = {
							has_trait = beauty_good
							has_trait = beauty_bad
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = beauty_good_1
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_intellect_good_1
						}
						NOR = {
							has_trait = intellect_good
							has_trait = intellect_bad
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = intellect_good_1
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_physique_good_1
						}
						NOR = {
							has_trait = physique_good
							has_trait = physique_bad
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = physique_good_1
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_fecund
						}
						NOR = {
							has_trait = infertile
							has_trait = fecund
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = fecund
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_giant
						}
						NOR = {
							has_trait = giant
							has_trait = dwarf
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = giant
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_dwarf
						}
						NOR = {
							has_trait = giant
							has_trait = dwarf
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = dwarf
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_albino
						}
						NOT = {
							has_trait = albino
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = albino
					}
				}
				else_if = {
					limit = {
						scope:child.dynasty = {
							exists = var:blood_legacy_4_scaly
						}
						NOT = {
							has_trait = scaly
						}
					}
					random = {
						chance = blood_legacy_4_chance
						modifier = {
							add = blood_legacy_4_extra_player_chance
							any_parent = { is_ai = no }
						}
						add_trait = scaly
					}
				}
			}
		}

		hidden_effect = { # Track El Cid's Lineage (FP2) for achievement
			if = {
				limit = { 
					OR = {
						scope:real_father = { has_character_flag = fp2_el_cid_blood_relation_legacy }
						scope:mother = { has_character_flag = fp2_el_cid_blood_relation_legacy }
					}
				}				
				scope:child = { add_character_flag = fp2_el_cid_blood_relation_legacy }
			}
		}
		#Enters into blood brotherhood family bond
		scope:child = {
			if = {
				limit = {
					any_parent = {
						has_blood_brother = yes
						any_relation = {
							type = blood_brother
							NOT = {
								is_close_family_of = scope:child
							}
						}
					}
				}
				every_parent = {
					limit = {
						has_blood_brother = yes
						any_relation = {
							type = blood_brother
							NOT = {
								is_close_family_of = scope:child
							}
						}	
					}
					random_relation = {
						type = blood_brother
						save_scope_as = other_blood_brother
					}
				}
				mpo_blood_brother_new_family_member_opinion_effect = yes
			}
		}
	}
}

# called for the mother when a pregnancy reaches the "revealed" status
# scope:mother is the pregnant mother
# scope:father is the assumed father
# scope:real_father is the biological father
on_pregnancy_mother = {
	events = {
		pregnancy.0001 # Universal hidden maintenance event
		pregnancy.1001 # Mother gives birth to current husband's child.
		pregnancy.1101 # Mother gives birth to ex-husband's child.
		pregnancy.2001 # Unwed mother gives birth to bastard child.
		pregnancy.2002 # Married mother gives birth to bastard child.
		pregnancy.2050 # Married mother in same-sex relationship.
	}
}

# called for the father when a pregnancy reaches the "revealed" status
# scope:mother is the pregnant mother
# scope:father is the assumed father
# scope:real_father is the biological father
on_pregnancy_father = {
	events = {
	
	}
}

# called for the mother when a pregnancy ends prematurely
# scope:mother is the pregnant mother
# scope:father is the assumed father
# scope:real_father is the biological father
on_pregnancy_ended_mother = {
	events = {
	
	}
}
